﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSYSTEM
{
    public partial class FMI_ACTIONWARRING : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        DBCache myCache = new DBCache();
        private int iGridView = 0;

        public FMI_ACTIONWARRING()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string sDate1 = uiDate1.Text;
            string sDate2 = uiDate2.Text;
            string SQL = "select  a.fupddate,b.username,s.fpoint_name,g.fequipmenttag fconname"
             + ", ISNULL(c.fequipmenttag, fcabbarcode) fcabname"
             + ",ISNULL(d.fequipmenttag, fboxbarcode) fboxname"
             + ",case faction when 0 then '取出' when 3 then '归还' when 1 then '入库' when 7 then '出库' when 8 then '盘点' end faction"
             + ",case fbiztype when 2 then '取' else '还' end fbiztype"
             + ", frfidbarcode, ISNULL(e.fgoodname, a.fgoodno) fgoodname,fmessage"
             + "  from i_warehouse_dtl a"
             + " left join am_sys_user b on b.id = a.foperator"
             + " left join i_ts_cabinet c on c.fequipmentbarcode = a.fcabbarcode"
             + " left join i_ts_warehouse d on d.fequipmentbarcode = a.fboxbarcode"
             + " left join i_ts_good_info e on e.fgoodno = a.fgoodno"
             + " left join i_ts_controlbox f on f.fcatbarcode=c.fequipmentbarcode"
             + " left join i_ts_box g on g.fequipmentbarcode=f.fequipmentbarcode"
             + " left join a_ts_point s on s.fpoint_no=c.fpoint_no"
             + " where 1 = 1"
             + " and fwarringflag = 1  "
             + "  and convert(varchar(10),a.fupddate,120) between '" + sDate1 + "'"
             + "  and '" + sDate2 + "'";
            DataRowView dr1 = (DataRowView)cbuser.SelectedItem;
            if ((dr1 != null) && (cbuser.SelectedIndex > 0))
            {
                SQL = SQL + " and a.foperator=" + dr1.Row["id"].ToString();
            }

            DataRowView dr2 = (DataRowView)cbfpoint_no.SelectedItem;
            if ((dr2 != null) && (cbfpoint_no.SelectedIndex > 0))
            {
                SQL = SQL + " and c.fpoint_no='" + dr2.Row["fpoint_no"].ToString() + "'";
            }

            DataRowView dr3 = (DataRowView)uibox.SelectedItem;
            if ((dr3 != null) && (uibox.SelectedIndex > 0))
            {
                SQL = SQL + " and g.fequipmentbarcode='" + dr3.Row["fequipmentbarcode"].ToString() + "'";
            }

            DataRowView dr4 = (DataRowView)cbaction.SelectedItem;
            if ((dr4 != null) && (cbaction.SelectedIndex > 0))
            {
                SQL = SQL + " and a.faction='" + dr4.Row["fdictno"].ToString() + "'";
            }

            if (uicabbarcode.Text != "")
            {
                SQL = SQL + " and c.fequipmentbarcode='"+uicabbarcode.Tag.ToString()+"'";
            }

            SQL = SQL + "  order by a.fupddate";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();

            rowindex = -1;
        }
        private void FMI_ACTIONWARRING_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys02.Visible = false; sys02.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys10.Visible = false; sys10.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            uiDate1.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uiDate2.Text = DateTime.Now.ToString("yyyy-MM-dd");
            uicabbarcode.Tag = "";

            //下拉框
            DataTable myUser = myCache.doGetDict("AM_SYS_USER", "");

            DataRow dr2 = myUser.NewRow();
            dr2["username"] = "请选择";
            dr2["id"] = "0";
            myUser.Rows.InsertAt(dr2, 0);
            cbuser.DataSource = myUser;
            cbuser.DisplayMember = "username";
            cbuser.ValueMember = "id";
            cbuser.SelectedIndex = 0;

            //业务类型
            DataTable myTs_Point = myCache.doGetDict("A_TS_POINT", "fpoint_flag=0");

            DataRow dr1 = myTs_Point.NewRow();
            dr1["fpoint_name"] = "请选择";
            dr1["fpoint_no"] = "0";
            myTs_Point.Rows.InsertAt(dr1, 0);
            cbfpoint_no.DataSource = myTs_Point;
            cbfpoint_no.DisplayMember = "fpoint_name";
            cbfpoint_no.ValueMember = "fpoint_no";
            cbfpoint_no.SelectedIndex = 0;

            //业务类型 
            DataTable myaction = myCache.doGetDict("S_TS_DICT", "FDICTTYPE=3011");

            DataRow dr4 = myaction.NewRow();
            dr4["fdictname"] = "请选择";
            dr4["fdictno"] = "0";
            myaction.Rows.InsertAt(dr4, 0);
            cbaction.DataSource = myaction;
            cbaction.DisplayMember = "fdictname";
            cbaction.ValueMember = "fdictno";
            cbaction.SelectedIndex = 0;

            //储物柜
            DataTable myTs_BOX = StaticClass.myDB.query(" select fequipmentbarcode,fequipmenttag from i_ts_box");

            DataRow dr3 = myTs_BOX.NewRow();
            dr3["fequipmenttag"] = "请选择";
            dr3["fequipmentbarcode"] = "0";
            myTs_BOX.Rows.InsertAt(dr3, 0);
            uibox.DataSource = myTs_BOX;
            uibox.DisplayMember = "fequipmenttag";
            uibox.ValueMember = "fequipmentbarcode";
            uibox.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        private void cbaction_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbuser_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uiBtn1_Click(object sender, EventArgs e)
        {
            //弹窗选择储物柜
            uicabbarcode.Text = "";
            uicabbarcode.Tag = "";

            string DATA = doPOPWINDOS("储物柜查询"
                , "fequipmentbarcode,储物柜编号,121;fequipmenttag,储物柜名称,141"
                , "select fequipmentbarcode,fequipmenttag,fmacno from i_ts_cabinet where 1=1"
                , " order by fequipmentbarcode"
                , 0);
            Console.WriteLine("当前返回:" + DATA);

            if ((DATA is null) || (DATA.Length == 0))
            {
                return;
            }
            string[] strArray = DATA.Split(',');

            uicabbarcode.Text = strArray[1];
            uicabbarcode.Tag = strArray[0];
        }

        private void cbfpoint_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uibox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }
    }
}
